package com.liesheng.haylou.app;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.ComponentName;
import android.os.Bundle;

import com.liesheng.daemonlib.service.DaemonEnv;
import com.liesheng.haylou.BuildConfig;
import com.liesheng.haylou.bean.UserUpdateBean;
import com.liesheng.haylou.db.DBManager;
import com.liesheng.haylou.service.TraceServiceImpl;
import com.liesheng.haylou.utils.sp.SpKey;
import com.liesheng.haylou.utils.sp.SpUtil;

import java.util.ArrayList;
import java.util.List;

public class HyApplication extends Application {
    public static boolean DEBUG_MODE = true;//是否是DEBUG模式
    public static HyApplication mApp;
    private boolean isRunInBackground;
    private UserUpdateBean userUpdateBean;//用户信息

    @Override
    public void onCreate() {
        super.onCreate();
//        DEBUG_MODE = BuildConfig.DEBUG_MODE;
        mApp = this;
        DBManager.getInstance().init();
        DaemonEnv.initialize(this, TraceServiceImpl.class, DaemonEnv.DEFAULT_WAKE_UP_INTERVAL);
//        initBackgroundCallBack();

    }

    public UserUpdateBean getUserUpdateBean() {
        return userUpdateBean;
    }

    public void setUserUpdateBean(UserUpdateBean userUpdateBean) {
        this.userUpdateBean = userUpdateBean;
    }

    public static HyApplication getInstance() {
        return mApp;
    }

    //判断activity是否在前台运行
    public boolean isForeground(Class activity) {
        String className = activity.getName();
        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
        if (list != null && list.size() > 0) {
            ComponentName cpn = list.get(0).topActivity;
            if (className.equals(cpn.getClassName()))
                return true;
        }
        return false;
    }


    /**
     * 从后台回到前台需要执行的逻辑
     *
     * @param activity
     */
    private void back2App(Activity activity) {
        isRunInBackground = false;
        //停止后台搜索蓝牙的服务
        TraceServiceImpl.stopService();
    }

    /**
     * 离开应用 压入后台或者退出应用
     *
     * @param activity
     */
    private void leaveApp(Activity activity) {
        isRunInBackground = true;
        //开启后台搜索蓝牙的服务
        TraceServiceImpl.sShouldStopService = false;
        DaemonEnv.startServiceMayBind(TraceServiceImpl.class);
    }

    public static HyApplication getContext() {
        return mApp;
    }

    private int activityCount;

    private void initBackgroundCallBack() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            }

            @Override
            public void onActivityStarted(Activity activity) {
                activityCount++;
                if (isRunInBackground) {
                    //应用从后台回到前台 需要做的操作
                    back2App(activity);
                }
            }

            @Override
            public void onActivityResumed(Activity activity) {
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityStopped(Activity activity) {
                activityCount--;
                if (activityCount == 0) {
                    //应用进入后台 需要做的操作
                    leaveApp(activity);
                }
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
            }
        });
    }


}
